//
//  CheckOnViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-6-28.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "CheckOnViewController.h"
#import "NetworkAccessManager.h"
#import "NSDate+WQCalendarLogic.h"
#import "WQCalendarTileView.h"
#import "UIAlertView+Kirinrin.h"

@interface CheckOnViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *previous;
@property (weak, nonatomic) IBOutlet UIButton *next;

@property (weak, nonatomic) IBOutlet UILabel *checkInTime;
@property (weak, nonatomic) IBOutlet UILabel *checkOutTime;

@property (weak, nonatomic) IBOutlet WQCalendarGridView *calContent;

@property (strong, nonatomic)NSArray *checkOnData;

@end

@implementation CheckOnViewController{
    NSDateFormatter *dateFormatter1;
    NSDateFormatter *dateFormatter2;
    
    NSDate *displayDate;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dateFormatter1 = [[NSDateFormatter alloc]init];
    [dateFormatter1 setDateFormat:@"yyyy年MM月"];
    
    dateFormatter2 = [[NSDateFormatter alloc]init];
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd"];
    
    displayDate = [NSDate date];
    
    [self setTitleLableAndButtonLable];
    
    
    
    [self loadRemoteData:[NSDate date] init:YES];
    
    self.calContent.displayDate = [NSDate date];
    self.calContent.delegate = self;
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


-(void)loadRemoteData:(NSDate *)date init:(BOOL)init{
    NSString *uriString = [NSString stringWithFormat:@"CheckOn/%@.json", self.appDelegate.user[@"childId"]];
    NSDictionary *parameters = @{@"key": self.appDelegate.accessKey, @"checkOnDate": [dateFormatter2 stringFromDate:date]};
    
    [[NetworkAccessManager shareManager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            _checkOnData = responseObject[@"data"];
            _calContent.checkOnData = _checkOnData;
            [_calContent reloadData];
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}



- (IBAction)previousMonthAction:(id)sender {
    displayDate = [displayDate dayInThePreviousMonth];
    self.calContent.displayDate = displayDate;
    [self loadRemoteData:displayDate init:NO];
    
    [self setTitleLableAndButtonLable];
}
- (IBAction)nextMonthAction:(id)sender {
    displayDate = [displayDate dayInTheFollowingMonth];
    self.calContent.displayDate = displayDate;
    [self loadRemoteData:displayDate init:NO];
    
    [self setTitleLableAndButtonLable];
}

/**
 *  更新日期label和左右的button上的月份
 */
-(void)setTitleLableAndButtonLable{
    [self.titleLabel setText:[dateFormatter1 stringFromDate:displayDate]];
    [self.previous setTitle:[[displayDate dayInThePreviousMonth]monthStringWithChinese]forState:UIControlStateNormal];
    [self.next setTitle:[[displayDate dayInTheFollowingMonth]monthStringWithChinese]forState:UIControlStateNormal];
}

#pragma mark - WQCalendarGridViewDataSource & delegate
- (void)gridView:(WQCalendarGridView *)gridView didSelectAtCell:(WQCalendarTileView *)cellView{
    NSLog(@"selected cal cell");
    if (cellView.checkOnData) {
        
        NSString *checkInString =cellView.checkOnData[@"checkInTime"];
        if (checkInString.length>5) {
            checkInString = [checkInString substringToIndex:5];
        }
        NSString *checkOutString = cellView.checkOnData[@"checkOutTime"];
        if (checkOutString.length>5) {
            checkOutString = [checkOutString substringToIndex:5];
        }
        
        
        [_checkInTime setText:checkInString];
        [_checkOutTime setText:checkOutString];
    }else{
        [_checkInTime setText:@"--:--"];
        [_checkOutTime setText:@"--:--"];
    }
}

@end
